<?php
/**
 * Created by PhpStorm.
 * User: 申霖
 * Date: 2019/8/25 0025
 * Time: 上午 10:24
 */
defined('BASEPATH') or exit('No direct script access allowed');

class Index extends MY_Controller
{
	/**
	 * 首页
	 * @author 申霖
	 * @time 2019/8/25 0025 上午 10:33
	 */
	public function home()
	{
		//读取配置，未配置则要求配置
		$this->config->load('sevenOxen');
		$ak     = $this->config->item('ak');
		$sk     = $this->config->item('sk');
		$bucket = $this->config->item('bucket');
		$domain = $this->config->item('domain');
		if (!$ak or !$sk or !$bucket or !$domain) {
			$base_url = $this->config->item('base_url');
			$this->load->helper('url');
			redirect($base_url . '/system.html');
		}
		//使用缓存
		$this->load->driver('cache');
		$serverList = $this->cache->get('serverList');
		if (!$serverList) {
			$serverList = $this->data($ak, $sk, $bucket, $domain);
			$this->cache->save('serverList', $serverList, 300);
		}
		$data['data'] = $serverList;
		$this->load->view('index/home', $data);
	}

	/**
	 * 获取资源列表
	 * @param $ak
	 * @param $sk
	 * @param $bucket
	 * @param $domain
	 * @return mixed
	 * @author 申霖
	 * @time 2019/8/27 0027 下午 9:29
	 */
	public function data($ak, $sk, $bucket, $domain)
	{
		$this->load->library("Qiniu");
		// 构建鉴权对象
		$auth = new Qiniu\Auth($ak, $sk);
		// 配置
		$config = new \Qiniu\Config();
		// 管理资源
		$bucketManager = new \Qiniu\Storage\BucketManager($auth, $config);
		// 获取空间名称
		$res  = $bucketManager->listFiles($bucket);
		$info = $res[0]['items'];
		$this->load->helper('common');
		foreach ($info as $k => $v) {
			$info[$k]['fsize']   = trans_byte($v['fsize']);
			$info[$k]['putTime'] = date('Y-m-d H:i:s', substr($v['putTime'], 0, 10));
			$info[$k]['type']    = change_type($v['type']);
			$info[$k]['status']  = change_status($v['status']);
			$info[$k]['domain']  = $domain;
		}
		return $info;
	}


	/**
	 * 404 错误页面
	 * @author 申霖
	 * @time 2019/8/25 0025 上午 10:57
	 */
	public function error()
	{
		$this->load->view('index/404');
	}

	/**
	 * 项目描述
	 * @author 申霖
	 * @time 2019/8/26 0026 下午 10:39
	 */
	public function about()
	{
		$data['name'] = 1;
		$data['data'] = array(
			array('title' => '七牛云存储空间上传图片接口', 'url' => 'https://www.shenlin.ink/back/24.html'),
			array('title' => '七牛云存储空间删除图片', 'url' => 'https://www.shenlin.ink/back/38.html'),
			array('title' => '七牛云存储的AccessKey和SecretKey', 'url' => 'https://www.shenlin.ink/back/65.html'),
		);
		$this->load->view('index/about', $data);
	}

	/**
	 * 上传资源
	 * @author 申霖
	 * @time 2019/8/27 0027 下午 7:34
	 */
	public function add()
	{
		$this->load->helper(array('form', 'url'));
		$this->load->view('index/add', array('error' => ''));
	}

	/**
	 * 上传
	 * @throws Exception
	 * @author shenlin
	 * @time 2021/02/09 17:53
	 * @phone 13614048679
	 */
	public function upload()
	{
		$file = $_FILES['file'];
		/**
		 * array (size=5)
		 * 'name' => string 'Dj建强-国粤语慢歌连版抖音后来再听已是曲中人抒情串烧.mp4' (length=79)
		 * 'type' => string 'video/mp4' (length=9)
		 * 'tmp_name' => string 'C:\Windows\phpA577.tmp' (length=22)
		 * 'error' => int 0
		 * 'size' => int 29291987
		 */
		// 要上传图片的本地路径
		$filePath = $file['tmp_name'];
		//获取当前控制器名称
		// 上传到七牛后保存的文件名
		$key = $file['name'];
		$this->config->load('sevenOxen');
		$ak     = $this->config->item('ak');
		$sk     = $this->config->item('sk');
		$bucket = $this->config->item('bucket');
		$domain = $this->config->item('domain');
		// 构建鉴权对象
		$this->load->library("Qiniu");
		$auth = new \Qiniu\Auth($ak, $sk);
		// 要上传的空间
		$token = $auth->uploadToken($bucket);
		// 初始化 UploadManager 对象并进行文件的上传
		$uploadMgr = new \Qiniu\Storage\UploadManager();
		// 调用 UploadManager 的 putFile 方法进行文件的上传
		list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
		if ($err !== null) {
			echo json_encode(array('code' => 100, 'msg' => $err));
		} else {
			//返回图片的完整URL
			$ret = $domain . "/" . $ret['key'];
			echo json_encode(array('code' => 200, 'msg' => '上传成功', 'data' => $ret));
		}
	}

	/**
	 * 删除图片
	 * @param $path
	 * @author 申霖
	 * @time 2019/8/31 0031 上午 9:51
	 */
	public function del()
	{
		$path = $this->input->post("src");
		if (!$path) {
			echo json_encode(array('code' => 100, 'msg' => '参数错误'));
		}
		//接收存储地址
		$delFileName = $path;
		$this->config->load('sevenOxen');
		$ak     = $this->config->item('ak');
		$sk     = $this->config->item('sk');
		$bucket = $this->config->item('bucket');
		$this->load->library("Qiniu");
		$this->load->library("Qiniu");
		// 构建鉴权对象
		$auth = new Qiniu\Auth($ak, $sk);
		// 配置
		$config = new \Qiniu\Config();
		// 管理资源
		$bucketManager = new \Qiniu\Storage\BucketManager($auth, $config);
		// 删除文件操作
		$res = $bucketManager->delete($bucket, $delFileName);
		if (is_null($res)) {
			echo json_encode(array('code' => 200, 'msg' => '删除成功'));
		} else {
			echo json_encode(array('code' => 103, 'msg' => '删除失败'));
		}
	}
}
